/**
 * User: Amitav Roy
 * Date: 10/22/12
 * Time: 10:28 PM
 */

$(document).ready(function() {
	// Getting the ajax data as dropdown.
	$('select#category-selector').live('change', 'select#category-selector', function(event) {
		get_category($('#category-selector'));
	});

	// Click of the delete button
	$('.the-icons .icon-pencil').click(function() {
		var expense_id = $(this).parent('span').attr('data-id');

		// Calling the function for ajax call.
		get_edit_form_details(expense_id);

		// Opening the modal popup.
		$('#myModal').modal('toggle');
	});
});

/**
 * This is the function to get the sub category on ajax call.
 * @param $scope
 */
function get_category($scope) {
	var selected_cat = '';
	selected_cat = $($scope).attr('value');
	// console.log(selected_cat);
	$.ajax({
		url: base_url + 'transactions/get_terms',
		async: false,
		type: 'POST',
		data: 'cid=' + selected_cat,
		dataType: 'html',
		success: function(data) {
			$('#sub-cat-holder').html(data);
		}
	})
}

/**
 * This function will return the ajax data for
 * edit modal popup.
 * @param expense_id
 */
function get_edit_form_details(expense_id) {
	$.ajax({
		url: base_url + 'transactions/edit_expense_view',
		async: false,
		type: 'POST',
		data: 'eid=' + expense_id + '&valid=1',
		dataType: 'html',
		success: function(data) {
			console.log(data);
			$('#edit-modal-popup-container').html(data);
		}
	});
}